package com.dtp.core.support.runnable;

import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * 命名任务类
 *
 * @author yanhom
 * @since 1.0.6
 *
 * @remark muse
 */
public class NamedRunnable implements Runnable {
    private final Runnable runnable;
    private final String name;

    public NamedRunnable(Runnable runnable, String name) {
        this.runnable = runnable;
        this.name = name;
    }

    @Override
    public void run() {this.runnable.run();}

    public String getName() {return name;}

    public static NamedRunnable of(Runnable runnable, String name) {
        // 如果没有指定名称，则采用默认命名规则进行命名操作
        if (StringUtils.isBlank(name))
            name = runnable.getClass().getSimpleName() + "-" + UUID.randomUUID();

        return new NamedRunnable(runnable, name);
    }
}
